package com.woniuxy.micropermissionserver.rolemanage.service.impl;

import com.woniuxy.microcommonserver.bean.permission_hcz.Admin;
import com.woniuxy.microcommonserver.bean.permission_hcz.Role;
import com.woniuxy.micropermissionserver.rolemanage.mapper.RoleMapper;
import com.woniuxy.micropermissionserver.rolemanage.service.IRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class RoleServiceImpl implements IRoleService {

    @Resource
    private RoleMapper roleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void saveRole(Role role, List<Integer> permissionIds) {
        role.setVersion(0);
        roleMapper.saveRole(role);
        if (!permissionIds.isEmpty() && permissionIds.size() > 0) {
            roleMapper.batchSaveRolePermission(role, permissionIds);
        }
    }

    @Override
    public List<String> findByAdmin(Admin admin) {
        return roleMapper.findByAdmin(admin);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.findAll();
    }
}
